/*
package com.linewell;

import com.mongodb.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.*;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Configuration
@ImportResource({"classpath:serviceConf/spring-mongodb.xml"})
public class MongoConfig {
    //覆盖默认的MongoDbFacotry

    //配置文件
    @Autowired
    private Environment env;

    @Bean(name = "mongoDbFactory")
    public MongoDbFactory mongoDbFactory() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("baseservice.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("baseservice.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("baseservice.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("baseservice.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("baseservice.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("baseservice.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("baseservice.datasource.username"),
                env.getProperty("baseservice.datasource.database"),
                env.getProperty("baseservice.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("baseservice.datasource.database"));
        return mongoDbFactory;
    }

    @Bean(name = "mappingMongoConverter")
    public MappingMongoConverter mappingMongoConverter(MongoDbFactory mongoDbFactory, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }


    @Bean(name = "logMongoDbFactory")
    public MongoDbFactory logMongoDbFactory() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("syslogs.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("syslogs.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("syslogs.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("syslogs.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("syslogs.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("syslogs.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("syslogs.datasource.username"),
                env.getProperty("syslogs.datasource.database"),
                env.getProperty("syslogs.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("syslogs.datasource.database"));
        return mongoDbFactory;
    }

    @Bean(name = "logMappingMongoConverter")
    public MappingMongoConverter logMappingMongoConverter(MongoDbFactory logMongoDbFactory, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(logMongoDbFactory);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }

    @Bean(name = "userMongoDbFactory")
    public MongoDbFactory userMongoDbFactory() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("user.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("user.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("user.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("user.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("user.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("user.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("user.datasource.username"),
                env.getProperty("user.datasource.database"),
                env.getProperty("user.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("user.datasource.database"));
        return mongoDbFactory;
    }

    @Bean(name = "userMappingMongoConverter")
    public MappingMongoConverter userMappingMongoConverter(MongoDbFactory userMongoDbFactory, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(userMongoDbFactory);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }


    @Bean(name = "mongoTemplate")
    public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory,MappingMongoConverter mappingMongoConverter){
            return new MongoTemplate(mongoDbFactory,mappingMongoConverter);
    }

    @Bean(name = "logMongoTemplate")
    public MongoTemplate logMongoTemplate(MongoDbFactory logMongoDbFactory,MappingMongoConverter logMappingMongoConverter){
        return new MongoTemplate(logMongoDbFactory,logMappingMongoConverter);
    }

    @Bean(name = "userMongoTemplate")
    public MongoTemplate userMongoTemplate(MongoDbFactory userMongoDbFactory,MappingMongoConverter userMappingMongoConverter){
        return new MongoTemplate(userMongoDbFactory,userMappingMongoConverter);
    }



    @Bean(name = "mongo1")
    public MongoDbFactory mongo1() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("user.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("user.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("user.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("user.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("user.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("user.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("user.datasource.username"),
                env.getProperty("user.datasource.dbname1"),
                env.getProperty("user.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("user.datasource.dbname1"));
        return mongoDbFactory;
    }

    @Bean(name = "mappingMongoConverter1")
    public MappingMongoConverter mappingMongoConverter1(MongoDbFactory mongo1, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongo1);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }


    @Bean(name = "mongoTemplate1")
    public MongoTemplate mongoTemplate1(MongoDbFactory mongo1,MappingMongoConverter mappingMongoConverter1){
        return new MongoTemplate(mongo1,mappingMongoConverter1);
    }

    @Bean(name = "mongo2")
    public MongoDbFactory mongo2() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("user.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("user.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("user.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("user.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("user.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("user.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("user.datasource.username"),
                env.getProperty("user.datasource.dbname2"),
                env.getProperty("user.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("user.datasource.dbname2"));
        return mongoDbFactory;
    }

    @Bean(name = "mappingMongoConverter2")
    public MappingMongoConverter mappingMongoConverter2(MongoDbFactory mongo2, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongo2);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }


    @Bean(name = "mongoTemplate2")
    public MongoTemplate mongoTemplate2(MongoDbFactory mongo2,MappingMongoConverter mappingMongoConverter2){
        return new MongoTemplate(mongo2,mappingMongoConverter2);
    }

    @Bean(name = "mongo3")
    public MongoDbFactory mongo3() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("user.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("user.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("user.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("user.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("user.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("user.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("user.datasource.username"),
                env.getProperty("user.datasource.dbname3"),
                env.getProperty("user.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("user.datasource.dbname3"));
        return mongoDbFactory;
    }

    @Bean(name = "mappingMongoConverter3")
    public MappingMongoConverter mappingMongoConverter3(MongoDbFactory mongo3, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongo3);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }


    @Bean(name = "mongoTemplate3")
    public MongoTemplate mongoTemplate3(MongoDbFactory mongo3,MappingMongoConverter mappingMongoConverter3){
        return new MongoTemplate(mongo3,mappingMongoConverter3);
    }

    @Bean(name = "mongo4")
    public MongoDbFactory mongo4() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("user.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("user.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("user.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("user.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("user.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("user.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("user.datasource.username"),
                env.getProperty("user.datasource.dbname4"),
                env.getProperty("user.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("user.datasource.dbname4"));
        return mongoDbFactory;
    }

    @Bean(name = "mappingMongoConverter4")
    public MappingMongoConverter mappingMongoConverter4(MongoDbFactory mongo4, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongo4);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }


    @Bean(name = "mongoTemplate4")
    public MongoTemplate mongoTemplate4(MongoDbFactory mongo4,MappingMongoConverter mappingMongoConverter4){
        return new MongoTemplate(mongo4,mappingMongoConverter4);
    }

    @Bean(name = "mongo5")
    public MongoDbFactory mongo5() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("user.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("user.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("user.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("user.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("user.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("user.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("user.datasource.username"),
                env.getProperty("user.datasource.dbname5"),
                env.getProperty("user.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("user.datasource.dbname5"));
        return mongoDbFactory;
    }

    @Bean(name = "mappingMongoConverter5")
    public MappingMongoConverter mappingMongoConverter5(MongoDbFactory mongo5, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongo5);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }


    @Bean(name = "mongoTemplate5")
    public MongoTemplate mongoTemplate5(MongoDbFactory mongo5,MappingMongoConverter mappingMongoConverter5){
        return new MongoTemplate(mongo5,mappingMongoConverter5);
    }

    @Bean(name = "mongo6")
    public MongoDbFactory mongo6() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("user.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("user.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("user.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("user.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("user.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("user.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("user.datasource.username"),
                env.getProperty("user.datasource.dbname6"),
                env.getProperty("user.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("user.datasource.dbname6"));
        return mongoDbFactory;
    }

    @Bean(name = "mappingMongoConverter6")
    public MappingMongoConverter mappingMongoConverter6(MongoDbFactory mongo6, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongo6);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }


    @Bean(name = "mongoTemplate6")
    public MongoTemplate mongoTemplate6(MongoDbFactory mongo6,MappingMongoConverter mappingMongoConverter6){
        return new MongoTemplate(mongo6,mappingMongoConverter6);
    }

    @Bean(name = "mongo7")
    public MongoDbFactory mongo7() {
        //客户端配置（连接数，副本集群验证）
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.requiredReplicaSetName("rs0");
        builder.maxWaitTime(Integer.parseInt(env.getProperty("user.datasource.maxWaitTime")));
        builder.writeConcern(WriteConcern.valueOf(env.getProperty("user.datasource.writeConcern")));
        builder.connectTimeout(Integer.parseInt(env.getProperty("user.datasource.connectTimeout")));
        builder.socketTimeout(Integer.parseInt(env.getProperty("user.datasource.socketTimeout")));
        builder.socketKeepAlive(Boolean.parseBoolean(env.getProperty("user.datasource.socketKeepAlive")));
        MongoClientOptions mongoClientOptions = builder.build();
        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : env.getProperty("user.datasource.host").split(",")) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.valueOf(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        // 连接认证
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(env.getProperty("user.datasource.username"),
                env.getProperty("user.datasource.dbname7"),
                env.getProperty("user.datasource.password").toCharArray());
        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, Collections.singletonList(mongoCredential), mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, env.getProperty("user.datasource.dbname7"));
        return mongoDbFactory;
    }

    @Bean(name = "mappingMongoConverter7")
    public MappingMongoConverter mappingMongoConverter7(MongoDbFactory mongo7, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongo7);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }


    @Bean(name = "mongoTemplate7")
    public MongoTemplate mongoTemplate7(MongoDbFactory mongo7,MappingMongoConverter mappingMongoConverter7){
        return new MongoTemplate(mongo7,mappingMongoConverter7);
    }

}
*/
